/**
 * Created by lwy on 2017/7/23.
 */

//可选择年月日
$('.layer-date').datetimepicker({
    language:  'zh-CN',
    minView: 'month',
    format: 'yyyy-mm-dd',
    autoclose: true,
    startView: 2,
    showMeridian: 1,
    pickerPosition: "bottom-right"
});

//个人收益
var page = 1;
var configName;
var id;
var name,
    value,
    memo;

//编辑
$('#save2').click(function(){
    $.ajax({
        url:'/crm/couponC/editCoupon.json',
        dataType:'json',
        data:{
            token:localStorage.getItem('crmtoken'),
            name:$('#editName').val(),
            type:$('#editType').val(),
            startTime:$('#editStartTime').val(),
            endTime:$('#editEndTime').val(),
            money:$('#editMoney').val(),
            state:$('#editState').val(),
            id:id
        },
        type:'post',
        success:function(data){
            if(data.code==-1){
                window.parent.location.href="/crm/login.jsp";
            }
            if(data.code==0){
                alert(data.msg);
            }
            if(data.code==1){
                alert('编辑成功');
                $('#myModal4').removeClass('fade in');
                $('#myModal4').addClass('fade');
                $('#myModal4').attr('aria-hidden',true);
                $('#myModal4').css('display','none');
                $('.modal-backdrop').css('display','none');
                page=1;
                doAjax();
            }
        }
    })
})

function doAjax(){
    $.ajax({
        url:'/crm/couponC/searchCoupon.json',
        dataType:'json',
        data:{
            token:localStorage.getItem('crmtoken'),
            pageNo:page,
            state:$("#state").val(),
            type:'post'
        },
        success: function (data) {
            if(data.code==-1){
                window.parent.location.href="/crm/login.jsp";
            }
            if(data.code==0){
                alert(data.msg);
            }
            if(data.code==1){
                $('#record').html(data.datas.recordsTotal);
                $('#withdraw-record').empty();
                for(var i=0;i<=data.datas.datas.length-1;i++){
                    $('<tr class="gradeA odd">'+
                        '<td class="sorting_1 withdrawId" style="text-decoration: underline;cursor: pointer;">'+data.datas.datas[i].id+'</td>'+
                        '<td class=" ">'+data.datas.datas[i].name+'</td>'+
                        '<td class=" ">'+data.datas.datas[i].type+'</td>'+
                        '<td class=" ">'+data.datas.datas[i].startTime+'</td>'+
                        '<td class=" ">'+data.datas.datas[i].endTime+'</td>'+
                        '<td class=" ">'+data.datas.datas[i].money+'</td>'+
                        '<td class=" ">'+data.datas.datas[i].state+'</td>'+
                        '<td class="withdrawId"  data-toggle="modal" data-target="#myModal4" style="text-decoration: underline;cursor: pointer;" id="'+data.datas.datas[i].id+'" name="'+data.datas.datas[i].name+'" type="'+data.datas.datas[i].type+'" startTime="'+data.datas.datas[i].startTime+'" endTime="'+data.datas.datas[i].endTime+'" state="'+data.datas.datas[i].state+'" money="'+data.datas.datas[i].money+'">编辑</td>'+
                        '</tr>').appendTo($('#withdraw-record'));
                }
                $(".pagination").createPage({
                    totalPage:data.datas.totalPage,
                    currPage:data.datas.nowPage,
                    turndown:'false',
                    backFn:function(p){
                        page=p;
                        doAjax();
                    }
                });
                //id处理
                $('.withdrawId').click(function(){
                    $.ajax({
                        url:'/crm/couponC/detailCoupon.json',
                        dataType:'json',
                        data:{
                            token:localStorage.getItem('crmtoken'),
                            pageNo:page,
                            id:$(this).attr('id'),
                            type:'post'
                        },
                        success: function (data) {
                            if(data.code==1){
                                id = data.datas.id;
                                name = data.datas.name;
                                type = data.datas.type;
                                startTime = data.datas.startTime;
                                endTime = data.datas.endTime;
                                money = data.datas.money;
                                state=data.datas.state;
                                $('#editName').val(name);
                                $('#editType').val(type);
                                $('#editStartTime').val(startTime)
                                $('#editEndTime').val(endTime)
                                $('#editMoney').val(money)
                                $('#editState').val(state)

                                if(type==2){
                                    $('#editMoney').attr("readonly","readonly");
                                    $('#editMoney').val("");
                                }else {
                                    $('#editMoney').removeAttr("readonly");
                                }

                            }
                        }});
                })
            }
        }
    })
}

$(function(){
    doAjax();
})
$('#editType').change(function () {
    if($('#editType').val()==2){
        $('#editMoney').attr("readonly","readonly");
        $('#editMoney').val("");
    }else {
        $('#editMoney').removeAttr("readonly");
    }

})

$('#addType').change(function () {
    if($('#addType').val()==2){
        $('#addMoney').attr("readonly","readonly");
        $('#addMoney').val("");
    }else {
        $('#addMoney').removeAttr("readonly");
    }

})
//查询
$('#refer').click(function(){
    configName = $('#configName').val();
    page=1;
    doAjax();
})

//添加
$('#save').click(function(){
    $.ajax({
        url:'/crm/couponC/addCoupon.json',
        dataType:'json',
        data:{
            token:localStorage.getItem('crmtoken'),
            name:$('#addName').val(),
            startTime:$('#addStartTime').val(),
            endTime:$('#addEndTime').val(),
            type:$('#addType').val(),
            money:$('#addMoney').val(),
            state:$('#addState').val()
        },
        type:'post',
        success:function(data){
            if(data.code==-1){
                window.parent.location.href="/crm/login.jsp";
            }
            if(data.code==0){
                alert(data.msg);
            }
            if(data.code==1){
                alert('添加成功');
                $('#myModal5').removeClass('fade in');
                $('#myModal5').addClass('fade');
                $('#myModal5').attr('aria-hidden',true);
                $('#myModal5').css('display','none');
                $('.modal-backdrop').css('display','none');
                page=1;
                doAjax();
            }
        }
    })
})